package com.studyv3.test.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyv3.common.core.page.PageQuery;
import com.studyv3.common.core.page.TableDataInfo;
import com.studyv3.common.core.page.TableDataInfoPlus;
import com.studyv3.common.utils.StringUtils;
import com.studyv3.test.domain.StudyBook;
import com.studyv3.test.domain.bo.StudyBookBo;
import com.studyv3.test.domain.vo.StudyBookVo;
import com.studyv3.test.mapper.StudyBookMapper;
import com.studyv3.test.service.IStudyBookService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @date 2024-04-24
 */
@RequiredArgsConstructor
@Service
public class StudyBookServiceImpl extends ServiceImpl<StudyBookMapper, StudyBook> implements IStudyBookService {

    private final StudyBookMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public StudyBookVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfoPlus<StudyBookVo> queryPageList(StudyBookBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StudyBook> lqw = buildQueryWrapper(bo);
        int total = Math.toIntExact(count(lqw));

        int page = (pageQuery.getPageNum()-1)*pageQuery.getPageSize();
        lqw.last("limit " + page + "," + pageQuery.getPageSize());
        List<StudyBookVo> result = baseMapper.selectVoList(lqw);

        TableDataInfoPlus<StudyBookVo> build = TableDataInfoPlus.build(result);
        build.setTotal(total);
        return  build;
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<StudyBookVo> queryList(StudyBookBo bo) {
        LambdaQueryWrapper<StudyBook> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StudyBook> buildQueryWrapper(StudyBookBo bo) {
//        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StudyBook> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), StudyBook::getName, bo.getName());
        lqw.in(bo.getTypesList() != null, StudyBook::getTypes, bo.getTypesList());
        lqw.eq(bo.getScore() != null, StudyBook::getScore, bo.getScore());
        if (ObjectUtil.isNotEmpty(bo.getCreateTimeBegin()) && ObjectUtil.isNotEmpty(bo.getCreateTimeEnd())){
            lqw.between(StudyBook::getCreateTime,bo.getCreateTimeBegin(),bo.getCreateTimeEnd());
        }
        // 若对 score排序
        if (ObjectUtil.isNotEmpty(bo.getIsOrderScore()) && bo.getIsOrderScore()) {
            lqw.orderByAsc(StudyBook::getScore);
        } else if ((ObjectUtil.isNotEmpty(bo.getIsOrderScore()) && bo.getIsOrderScore())) {
            lqw.orderByDesc(StudyBook::getScore);
        }
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(StudyBookBo bo) {
        StudyBook add = BeanUtil.toBean(bo, StudyBook.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(StudyBookBo bo) {
        StudyBook update = BeanUtil.toBean(bo, StudyBook.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StudyBook entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
